package ljl.alg.wangzheng_camp.round1.tree;

import commons.ListNode;
import commons.TreeNode;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * 不就层序遍历吗？逼逼赖赖的
 */
public class _04_03_list_of_depth {
    
    public ListNode[] listOfDepth(TreeNode tree) {
        if (tree == null) return new ListNode[0];
        List<ListNode> res = new ArrayList<>();
        LinkedList<TreeNode> q = new LinkedList<>();
        q.offer(tree);
        while (!q.isEmpty()) {
            int n = q.size();
            ListNode head = new ListNode();
            ListNode p = head;
            for (int i = 0; i < n; i++) {
                TreeNode node = q.poll();
                p.next = new ListNode(node.val);
                p = p.next;
                if (node.left != null) q.offer(node.left);
                if (node.right != null) q.offer(node.right);
            }
            res.add(head.next);
        }
        return res.toArray(new ListNode[0]);
    }
}
